package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.parm.BusinessLoginParm;
import com.ruoyi.system.exception.GlobalSysException;
import com.ruoyi.system.util.GlobalConstants;
import com.ruoyi.system.util.TokenUtil;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.service.ISysMemberService;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class SysMemberServiceImpl implements ISysMemberService 
{
    @Autowired
    private SysMemberMapper sysMemberMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public SysMember selectSysMemberById(String id)
    {
        return sysMemberMapper.selectSysMemberById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param sysMember 用户
     * @return 用户
     */
    @Override
    public List<SysMember> selectSysMemberList(SysMember sysMember)
    {
        return sysMemberMapper.selectSysMemberList(sysMember);
    }

    /**
     * 新增用户
     * 
     * @param sysMember 用户
     * @return 结果
     */
    @Override
    public int insertSysMember(SysMember sysMember)
    {
        SysMember re = sysMemberMapper.selectSysMemberByPhone(sysMember.getPhone());
        if(re!=null){
            throw new GlobalSysException("手机号码已经注册");
        }
        SysMember hasUsername = sysMemberMapper.selectSysMemberByUserName(sysMember.getUserName());
        if(hasUsername!=null){
            throw new GlobalSysException("用户名已经注册");
        }

        sysMember.setCreateTime(DateUtils.getNowDate());
        sysMember.setPassword(Md5Crypt.apr1Crypt(sysMember.getPassword(), GlobalConstants.PASSWORD_SALT));

        return sysMemberMapper.insertSysMember(sysMember);
    }

    /**
     * 修改用户
     * 
     * @param sysMember 用户
     * @return 结果
     */
    @Override
    public int updateSysMember(SysMember sysMember)
    {
        sysMember.setUpdateTime(DateUtils.getNowDate());
        return sysMemberMapper.updateSysMember(sysMember);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByIds(String[] ids)
    {
        return sysMemberMapper.deleteSysMemberByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberById(String id)
    {
        return sysMemberMapper.deleteSysMemberById(id);
    }

    @Override
    public SysMember Login(BusinessLoginParm businessLoginParm) throws Exception {
        SysMember result = sysMemberMapper.selectSysMemberByUserNameAndPhone(businessLoginParm.getUserName());

        if(result==null){
            throw new Exception("账号不存在");
        }

        String parPassword=Md5Crypt.apr1Crypt(businessLoginParm.getPassword(), GlobalConstants.PASSWORD_SALT);
        //     sysBusiness.setPassword(Md5Crypt.apr1Crypt(sysBusiness.getPassword(),"54gfdr434vdf43434fd343"));

        if(!parPassword.equals(result.getPassword())){
            throw new Exception("账号密码错误");
        }
        result.setPassword("");
        return result;
    }

    @Override
    public SysMember getUserInfo() {
        String token =  request.getHeader("Authorization");
        if(token!=null && !token.equals("")){
            String userName = TokenUtil.getUserNameFromTokenForMember(token);
            String retult = (String) redisTemplate.opsForValue().get("member_"+userName);

            if(retult!=null){
                SysMember obj = JSONObject.parseObject(retult,SysMember.class);
                return obj;
            }
        }
        return null;
    }

}
